package main

func findUnsortedSubarray(nums []int) int {
	max := -1 << 30
	left := 0
	right := -1
	for i, v := range nums {
		if v >= max {
			max = v
		} else {
			right = i
		}
	}
	min := 1 << 30
	for i := len(nums) - 1; i >= 0; i-- {
		v := nums[i]
		if v <= min {
			min = v
		} else {
			left = i
		}
	}
	return right - left + 1
}
